import store from '../../redux/index';
var hash = window.location.hash.split('/')[1] ? window.location.hash.split('/')[1] : '';

export function setStatus(state = localStorage.getItem('token') ? true : false, action) {
  switch (action.type) {
    case 'changestatus':
      return action.status;
    default:
      return state;
  }
}
export function changeNetwork(state = '', action) {
  switch (action.type) {
    case 'changenetwork':
      return action.network;
    default:
      return state;
  }
}
export function changeOrgid(state = '', action) {
  switch (action.type) {
    case 'changeorgid':
      return action.orgid;
    default:
      return state;
  }
}
export function changeOrgEnglishName(state = '', action) {
  switch (action.type) {
    case 'orgdata':
      let newState = JSON.parse(JSON.stringify(state));
      newState = action.orgData;
      return newState;
    default:
      return state;
  }
}
export function clickNetwork(state = localStorage.getItem('clicknetworkapproval'), action) {
  switch (action.type) {
    case 'clicknetwork':
      return action.clicknetwork;
    default:
      return state;
  }
}
export function changSandBox(state = Number(localStorage.getItem('sandbox')), action) {
  switch (action.type) {
    case 'changSandBox':
      return action.changesand;
    default:
      return state;
  }
}

export function PublicKey(state = sessionStorage.getItem('defaultValue'), action) {
  switch (action.type) {
    case 'changepublickey':
      return action.changepublickey;
    default:
      return state;
  }
}

export function getCertId(state = '', action) {
  switch (action.type) {
    case 'id':
      return action.Id;
    default:
      return state;
  }
}
export function secretName(state = '', action) {
  switch (action.type) {
    case 'secretName':
      return action.SecretName;
    default:
      return state;
  }
}
export function organdnet(state = {}, action) {
  switch (action.type) {
    case 'orgNet':
      var newState = JSON.parse(JSON.stringify(state));
      newState.org = action.org;
      return newState;
    case 'netOrg':
      var newState = JSON.parse(JSON.stringify(state));
      newState.net = action.net;
      return newState;
    default:
      return state;
  }
}

export function channelId(state = '', action) {
  switch (action.type) {
    case 'channelId':
      return action.channelid;
    default:
      return state;
  }
}
export function updateNet(state = '', action) {
  switch (action.type) {
    case 'updatenet':
      return action.onoff;
    default:
      return state;
  }
}
export function updateonoff(state = '', action) {
  switch (action.type) {
    case 'update':
      return action.onoff;
    default:
      return state;
  }
}
export function tipmessage(state = '', action) {
  switch (action.type) {
    case 'changemessage':
      return action.message;
    default:
      return state;
  }
}
export function loginout(state = '', action) {
  switch (action.type) {
    case 'loginout':
      localStorage.removeItem('userName');
      localStorage.removeItem('fillclickTime'); // 创建组织填写资料  获取验证码
      localStorage.removeItem('noUseclickTime'); // 证书管理 作废证书弹窗  获取验证码
      localStorage.removeItem('createKeyclickTime'); // 创建密钥 获取验证码
      localStorage.removeItem('changePwdclickTime'); // 密码修改  获取验证码
      localStorage.removeItem('changePhoneclickTime');
      localStorage.removeItem('token');
      localStorage.setItem('history', window.location.hash);
      setTimeout(function() {
        window.location.hash = '/';
        store.dispatch({
          type: 'changestatus',
          status: false
        });
        store.dispatch({
          type: 'netOrg',
          net: ''
        });
        store.dispatch({
          type: 'changenetwork',
          network: ''
        });
        store.dispatch({
          type: 'orgNet',
          org: ''
        });
        store.dispatch({
          type: 'changeorgid',
          orgid: ''
        });
        store.dispatch({
          type: 'orgdata',
          orgData: ''
        });
      }, 100);
      return state;
    default:
      return state;
  }
}
